﻿var WorkBasket;
(function (WorkBasket) {
    (function (Diagram) {
        var myDiagram;

        var nodata = {
            class: go.GraphLinksModel,
            linkFromPortIdProperty: "fromPort",
            linkToPortIdProperty: "toPort",
            nodeDataArray: [],
            linkDataArray: []
        };

        var data;

        function loadtextarea() {
            $('#mydata')[0].innerText = myDiagram.model.toJson();
        }
        Diagram.loadtextarea = loadtextarea;

        function init() {
            var $ = go.GraphObject.make;

            myDiagram = $(go.Diagram, "myDiagram", { allowDrop: true }); // must be true to accept drops from the Palette

            myDiagram.addDiagramListener("Modified", function (e) {
                var button = document.getElementById("SaveButton");
                if (button)
                    button.disabled = !myDiagram.isModified;
                var idx = document.title.indexOf("*");
                if (myDiagram.isModified) {
                    if (idx < 0)
                        document.title += "*";
                } else {
                    if (idx >= 0)
                        document.title = document.title.substr(0, idx);
                }
            });

            // helper definitions for node templates
            // Don't show shadows on mobile devices for performance reasons
            var shadows = !("ontouchstart" in window);

            function nodeStyle() {
                return {
                    // the Node.location is at the center of each node
                    locationSpot: go.Spot.Center,
                    //isShadowed: shadows,
                    //shadowColor: "#242424",
                    // handle mouse enter/leave events to show/hide the ports
                    mouseEnter: function (e, obj) {
                        showPorts(obj.part, true);
                    },
                    mouseLeave: function (e, obj) {
                        showPorts(obj.part, false);
                    }
                };
            }

            // Define a function for creating a "port" that is normally transparent.
            // The "name" is used as the GraphObject.portId, the "spot" is used to control how links connect
            // and where the port is positioned on the node, and the boolean "output" and "input" arguments
            // control whether the user can draw links from or to the port.
            function makePort(name, spot, output, input) {
                // the port is basically just a small circle that has a white stroke when it is made visible
                return $(go.Shape, "Circle", {
                    fill: "transparent",
                    stroke: null,
                    desiredSize: new go.Size(6, 6),
                    alignment: spot, alignmentFocus: spot,
                    portId: name,
                    fromSpot: spot, toSpot: spot,
                    fromLinkable: output, toLinkable: input,
                    cursor: "pointer"
                });
            }

            // define the Node template for regular nodes
            var lightText = 'whitesmoke';
            var darkText = '#454545';
            var startColor = "#79C900";
            var mainColor = "#00A9C9";
            var endColor = "#DC3C00";

            myDiagram.nodeTemplateMap.add("", $(go.Node, "Spot", nodeStyle(), new go.Binding("location", "loc", go.Point.parse).makeTwoWay(go.Point.stringify), $(go.Panel, "Auto", $(go.Shape, "Rectangle", { fill: mainColor, stroke: null }, new go.Binding("figure", "figure")), $(go.TextBlock, {
                font: "bold 11pt Helvetica, Arial, sans-serif",
                stroke: lightText,
                margin: 8,
                maxSize: new go.Size(160, NaN),
                wrap: go.TextBlock.WrapFit,
                editable: true
            }, new go.Binding("text", "text").makeTwoWay())), makePort("T", go.Spot.Top, false, true), makePort("L", go.Spot.Left, true, true), makePort("R", go.Spot.Right, true, true), makePort("B", go.Spot.Bottom, true, false)));

            myDiagram.nodeTemplateMap.add("Start", $(go.Node, "Spot", nodeStyle(), new go.Binding("location", "loc", go.Point.parse).makeTwoWay(go.Point.stringify), $(go.Panel, "Auto", $(go.Shape, "Octagon", { minSize: new go.Size(40, 60), fill: startColor, stroke: null }), $(go.TextBlock, "Start", {
                margin: 5,
                font: "bold 11pt Helvetica, Arial, sans-serif",
                stroke: lightText
            })), makePort("L", go.Spot.Left, true, false), makePort("R", go.Spot.Right, true, false), makePort("B", go.Spot.Bottom, true, false)));

            myDiagram.nodeTemplateMap.add("End", $(go.Node, "Spot", nodeStyle(), new go.Binding("location", "loc", go.Point.parse).makeTwoWay(go.Point.stringify), $(go.Panel, "Auto", $(go.Shape, "Octagon", { minSize: new go.Size(40, 60), fill: endColor, stroke: null }), $(go.TextBlock, "End", {
                margin: 5,
                font: "bold 11pt Helvetica, Arial, sans-serif",
                stroke: lightText
            })), makePort("T", go.Spot.Top, false, true), makePort("L", go.Spot.Left, false, true), makePort("R", go.Spot.Right, false, true)));

            myDiagram.nodeTemplateMap.add("Comment", $(go.Node, "Auto", nodeStyle(), new go.Binding("location", "loc", go.Point.parse).makeTwoWay(go.Point.stringify), $(go.Shape, "File", { fill: "#EFFAB4", stroke: null }), $(go.TextBlock, {
                margin: 5,
                maxSize: new go.Size(200, NaN),
                wrap: go.TextBlock.WrapFit,
                textAlign: "center",
                editable: true,
                font: "bold 12pt Helvetica, Arial, sans-serif",
                stroke: '#454545'
            }, new go.Binding("text", "text").makeTwoWay())));

            // replace the default Link template in the linkTemplateMap
            myDiagram.linkTemplate = $(go.Link, {
                routing: go.Link.AvoidsNodes,
                curve: go.Link.JumpOver,
                corner: 5, toShortLength: 4,
                relinkableFrom: true, relinkableTo: true, reshapable: true
            }, new go.Binding("points").makeTwoWay(), $(go.Shape, {
                isPanelMain: true,
                stroke: "gray", strokeWidth: 2
            }), $(go.Shape, {
                toArrow: "standard",
                stroke: null, fill: "gray"
            }), $(go.Panel, "Auto", { visible: false, name: "LABEL", segmentIndex: 2, segmentFraction: 0.5 }, new go.Binding("visible", "visible").makeTwoWay(), $(go.Shape, "RoundedRectangle", { fill: "#F8F8F8", stroke: null }), $(go.TextBlock, "Yes", {
                textAlign: "center",
                font: "10pt helvetica, arial, sans-serif",
                stroke: "#919191",
                margin: 2, editable: true
            }, new go.Binding("text", "text").makeTwoWay())));

            // make link labels visible if coming out of a "conditional" node
            myDiagram.addDiagramListener("LinkDrawn", function (e) {
                if (e.subject.fromNode.data.figure === "Diamond") {
                    var label = e.subject.findObject("LABEL");
                    if (label !== null)
                        label.visible = true;
                }
            });

            // temporary links used by LinkingTool and RelinkingTool are also orthogonal:
            myDiagram.toolManager.linkingTool.temporaryLink.routing = go.Link.Orthogonal;
            myDiagram.toolManager.relinkingTool.temporaryLink.routing = go.Link.Orthogonal;

            // load();  // load an initial diagram from some JSON text
            // initialize the Palette that is on the left side of the page
            var myPalette = $(go.Palette, "myPalette", {
                nodeTemplateMap: myDiagram.nodeTemplateMap,
                model: new go.GraphLinksModel([
                    { category: "Start", text: "Start" },
                    { text: "Step" },
                    { category: "End", text: "End" },
                    { category: "Comment", text: "Comment", figure: "RoundedRectangle" }
                ])
            });
        }
        Diagram.init = init;

        // Make all ports on a node visible when the mouse is over the node
        function showPorts(node, show) {
            var diagram = node.diagram;
            if (!diagram || diagram.isReadOnly || !diagram.allowLink)
                return;
            var it = node.ports;
            while (it.next()) {
                var port = it.value;
                port.stroke = (show ? "white" : null);
                //port.fill = (show ? "white" : null);
            }
        }

        // Show the diagram's model in JSON format that the user may edit
        function getDiagram() {
            var data = myDiagram.model.toJson();

            myDiagram.isModified = false;
            return data;
        }
        Diagram.getDiagram = getDiagram;
        function setDiagram(mydata) {
            data = mydata;
            if (data == "" || data == null || data == undefined) {
                data = nodata;
            }
            myDiagram.model = go.Model.fromJson(data);
            myDiagram.model.undoManager.isEnabled = true;
        }
        Diagram.setDiagram = setDiagram;
    })(WorkBasket.Diagram || (WorkBasket.Diagram = {}));
    var Diagram = WorkBasket.Diagram;
})(WorkBasket || (WorkBasket = {}));
//# sourceMappingURL=Diagram.js.map
